package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reggie.common.R;
import com.reggie.entity.ShoppingCart;
import com.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车的功能
 * @program: reggie
 * @author:
 * @create: 2022-10-12 10:26
 **/
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 购物车中信息的展示
     * */
    @GetMapping("/list")
    public R<List> list(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }
    /**
     * 添加到购物车
     * 如果提交的是菜品，前端提交的是dishId;如果是套餐的话，提交是setmealId
     * */
    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        shoppingCart.setUserId(userId);

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        //判断添加到购物车中的是菜品还是套餐
        if(shoppingCart.getDishId()!=null){
            //添加到购物车中的是菜品
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{
            //添加到购物车中的是套餐
            //查看购物车中是否有该套餐，有相同的套餐是数量上的加1，没有是菜品上的加1
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one=shoppingCartService.getOne(wrapper);
        if (one!=null){
            //如果购物车中有该套餐或者菜品，需要添加数量
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
        }else{
            //购物车没有相同的套餐或者菜品，直接进行保存
            shoppingCartService.save(shoppingCart);
        }


        return R.success("添加成功");

    }
    /**
     * 清空购物车中的信息
     * */
    @DeleteMapping("/clean")
    public R<String> clean(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(wrapper);
        return R.success("清除成功！");
    }
    /**
     * 修改购物车中菜品的数量
     * shoppingCart中存储的可能是dishId或者SetmealId
     * */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart,HttpSession session){
        //查询菜品的信息
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,userId);
        //判断是菜品还是套餐
        if(shoppingCart.getSetmealId()!=null){
            //套餐
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else{
            //菜品
            wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        //判断该菜品或者套餐的数量
        if (one.getNumber()>1){
            //大于1,执行修改的操作
            one.setNumber(one.getNumber()-1);
            shoppingCartService.updateById(one);
        }else{
            //数量为1，直接删除
            shoppingCartService.remove(wrapper);
        }
        System.out.println("修改成功！");
        return R.success("修改成功！");
    }
}
